/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Quick Controls module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.1
import QtQml.Models 2.1
import QtQuick.Controls 1.1
import QtQuick.Window 2.0
import SmartingEEGClientAgent 1.0
import SmartingEEGRecord 1.0


Rectangle {
    id:mainWindow
    width: Screen.width
    height: Screen.height
    property int batteryValue: 65
    color: "#000000"

    SmartingEEGClientAgent
    {
        id: eegAgent
        Component.onCompleted: init()
        onBufferOverloadedChanged: plotView.updatePlotRefreshing();
    }

    Item
    {
        id:toolbar
        width: parent.width
        anchors.top: parent.top
        height: parent.height * 0.14

        Image{
            id:toolbarImg
            width: parent.width
            height: parent.height * 0.5
            anchors.top: parent.top
            source: "images/toolbar3.png"
            Text{
                color: "white"
                font.pixelSize: parent.height*0.7
                text: "SMARTING"
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: parent.width * 0.08
            }
            Rectangle {
                height: parent.height * 0.6
                width: parent.width * 0.05
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: parent.right
                anchors.rightMargin: parent.width * 0.06
                color: "#444"
                opacity: 0.8
                Rectangle {
                    antialiasing: true
                    anchors.bottom: parent.bottom
                    color: "#00FF00"
                    height: parent.height * batteryValue / 100
                    width:  parent.width
                }
            }
        }

        Row
        {
            width: parent.width
            height: parent.height - toolbarImg.height
            anchors.top: toolbarImg.bottom
            TabBarStyle{
                id:tab1
                text: "Connect"
                tabSelected: view.currentIndex == 0
                borderRect:false
                width: parent.width/3
                height:parent.height
                MouseArea {
                    width: parent.width
                    height: parent.height
                    onClicked: view.currentIndex = 0
                }
            }

            TabBarStyle{
                id:tab2
                text: "Plot"
                tabSelected: view.currentIndex == 1
                borderRect:true
                width: parent.width/3
                height:parent.height
                MouseArea {
                    width: parent.width
                    height: parent.height
                    onClicked: view.currentIndex = 1
                }
            }

            TabBarStyle{
                id:tab3
                text: "Settings"
                tabSelected: view.currentIndex == 2
                borderRect:true
                width: parent.width/3
                height:parent.height
                MouseArea {
                    width: parent.width
                    height: parent.height
                    onClicked: view.currentIndex = 2
                }
            }
        }
    }

    Rectangle {
        focus:true
        color: "#000000"
        width: parent.width
        height: parent.height-toolbar.height
        anchors.top: toolbar.bottom

        ObjectModel {
            id: itemModel
            Rectangle{
                width: Screen.width
                height: Screen.height -toolbar.height
                color: "#000000"
                ConnectPage {id: connectPage}
            }
            Rectangle{
                width: Screen.width
                height: Screen.height-toolbar.height
                color: "#000000"
                PlotPage{
                    id:plotView
                }
            }
            Rectangle{
                width: Screen.width
                height: Screen.height -toolbar.height
                color: "#000000"
                SettingsPage {id: settingsView}
            }
        }

        ListView {
            id: view
            anchors.fill: parent
            model: itemModel
            interactive: false
            preferredHighlightBegin: 0; preferredHighlightEnd: 0
            highlightRangeMode: ListView.StrictlyEnforceRange
            highlightFollowsCurrentItem :true
            highlightMoveDuration:Screen.width
            orientation: ListView.Horizontal
            snapMode: ListView.SnapOneItem;
            flickDeceleration: 2000
            cacheBuffer: 200
        }


        Keys.onReleased: {
            event.accepted = true;
            if (view.currentIndex === 2)
                settingsView.popStackPage(event.key);
            else
            {
                if ((event.key === Qt.Key_Back)||(event.key ===Qt.Key_Escape))
                {
                    Qt.quit();
                }
            }

        }
    }
}
